<?php

namespace App\Services\Game\Other\Mail;

use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Mappers\Game\Other\Mail\PlayerMapper as DataMapper;
use App\Models\Game\Mail\Player as Mail;
use App\Models\Game\Mail\PlayerDictionary;
use App\Models\Game\Common\Dictionary;

class PlayerService extends Service
{
    use DatabaseMysql;

    /**
     * 获取数据列表
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Database\Eloquent\Collection[]|int[]
     */
    public function getList($request)
    {
        $query = Mail::query();

        // 筛选【平台】
        $query->whereHas('receiver', function ($query) use ($request) {
            $this->makeWhereParamOrg($request, $query);
        });
        // 筛选【搜索】
        if ($this->isValidParam($request, 'keyword')) {
            switch ($request->input('keyword_column')) {
                case 'account_id':
                    $query->whereHas('receiver', function ($query) use ($request) {
                        $query->where('account_id', $request->input('keyword'));
                    });
                    break;
                default:
                    $this->makeWhereParamSearch($request, $query);
            }
        }
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $query);
        // 筛选【类型】
        $this->makeWhereParam($request, 'type', $query);
        // 筛选【是否发送】
        $this->makeWhereParam($request, 'issend', $query);
        // 筛选【状态】
        $this->makeWhereParamBits($request, 'status', $query);

        // 数据条数
        $total = (clone $query)->count();
        // 排序分页
        $this->makeOrderParam($request, $query)->makePaginationParam($request, $query);
        // 查询数据
        $collection = $query->with([
            'receiver:id,account_id,plat_code', 'receiver.org:code,name',
            'titles:id,content', 'bodys:id,content'
        ])->get();

        return [
            $collection,
            $total
        ];
    }

    /**
     * 创建数据
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function store($request)
    {
        // 验证内容和参数匹配
        Dictionary::validate(
            $request->input('body_ids', []),
            $request->input('body_param', [])
        );

        $mails = collect();
        // 数据映射
        $data = DataMapper::make($request);
        // 发送玩家
        $userIds = $request->input('user_ids');

        // 循环处理多玩家
        foreach ($userIds as $uid) {
            $data->offsetSet('receiver_id', $uid);
            // 创建数据
            $mail = Mail::create($data->toArray());
            // 标题
            $mail->titles()->sync(array_fill_keys(
                $request->input('title_ids', []),
                ['mail_content_type' => PlayerDictionary::TYPE_TITLE]
            ));
            // 内容
            $mail->bodys()->sync(array_fill_keys(
                $request->input('body_ids', []),
                ['mail_content_type' => PlayerDictionary::TYPE_BODY]
            ));

            $mails->push($mail);
        }

        return $mails;
    }

    /**
     * 更新数据
     *
     * @param int $id
     * @param \Illuminate\Http\Request $request
     * @return \App\Models\Game\Mail\Player
     */
    public function update($id, $request)
    {
        // 验证内容和参数匹配
        Dictionary::validate(
            $request->input('body_ids', []),
            $request->input('body_param', [])
        );

        // 数据映射
        $data = DataMapper::make($request);
        // 发送玩家
        $data->offsetSet('receiver_id', $request->input('receiver_id'));
        // 查询数据
        $mail = Mail::findOrFail($id);
        // 修改数据
        $mail->fill($data->toArray())->save();

        // 标题
        $mail->titles()->sync(array_fill_keys(
            $request->input('title_ids', []),
            ['mail_content_type' => PlayerDictionary::TYPE_TITLE]
        ));
        // 内容
        $mail->bodys()->sync(array_fill_keys(
            $request->input('body_ids', []),
            ['mail_content_type' => PlayerDictionary::TYPE_BODY]
        ));


        return $mail;
    }
}
